package com.melesta.payment.amazon;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import com.amazon.inapp.purchasing.BasePurchasingObserver;
import com.amazon.inapp.purchasing.GetUserIdResponse;
import com.amazon.inapp.purchasing.GetUserIdResponse.GetUserIdRequestStatus;
import com.amazon.inapp.purchasing.Item.ItemType;
import com.amazon.inapp.purchasing.ItemDataResponse;
import com.amazon.inapp.purchasing.Offset;
import com.amazon.inapp.purchasing.PurchaseResponse;
import com.amazon.inapp.purchasing.PurchaseResponse.PurchaseRequestStatus;
import com.amazon.inapp.purchasing.PurchaseUpdatesResponse;
import com.amazon.inapp.purchasing.PurchaseUpdatesResponse.PurchaseUpdatesRequestStatus;
import com.amazon.inapp.purchasing.PurchasingManager;
import com.amazon.inapp.purchasing.Receipt;
import com.melesta.engine.DatabaseHelper;
import com.melesta.engine.DatabaseHelper.IObjectFilter;
import com.melesta.engine.EngineActivity;
import com.melesta.engine.Error;
import com.melesta.engine.Log;
import com.melesta.payment.PaymentManager;
import com.melesta.payment.PaymentTransaction;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class PurchasingObserver extends BasePurchasingObserver
{
  private static final String OFFSET = "offset";
  private static final String TAG = "Amazon-IAP";
  private final Context mContext;
  private final PaymentSystem mPaymentSystem;
  private String mUserId;

  public PurchasingObserver(Context paramContext, PaymentSystem paramPaymentSystem)
  {
    super(paramContext);
    this.mContext = paramContext;
    this.mPaymentSystem = paramPaymentSystem;
  }

  private SharedPreferences.Editor getSharedPreferencesEditor()
  {
    return getSharedPreferencesForCurrentUser().edit();
  }

  private SharedPreferences getSharedPreferencesForCurrentUser()
  {
    return this.mContext.getSharedPreferences(this.mUserId, 0);
  }

  public void onGetUserIdResponse(GetUserIdResponse paramGetUserIdResponse)
  {
    Log.v("Amazon-IAP", "onGetUserIdResponse recieved: Response -" + paramGetUserIdResponse);
    Log.v("Amazon-IAP", "RequestId:" + paramGetUserIdResponse.getRequestId());
    Log.v("Amazon-IAP", "IdRequestStatus:" + paramGetUserIdResponse.getUserIdRequestStatus());
    if (paramGetUserIdResponse.getUserIdRequestStatus() == GetUserIdResponse.GetUserIdRequestStatus.SUCCESSFUL)
    {
      this.mUserId = paramGetUserIdResponse.getUserId();
      PurchasingManager.initiatePurchaseUpdatesRequest(Offset.fromString(this.mContext.getApplicationContext().getSharedPreferences(this.mUserId, 0).getString("offset", Offset.BEGINNING.toString())));
      return;
    }
    this.mUserId = null;
  }

  public void onItemDataResponse(ItemDataResponse paramItemDataResponse)
  {
    Log.v("Amazon-IAP", "onItemDataResponse recieved");
    Log.v("Amazon-IAP", "ItemDataRequestStatus" + paramItemDataResponse.getItemDataRequestStatus());
    Log.v("Amazon-IAP", "ItemDataRequestId" + paramItemDataResponse.getRequestId());
  }

  public void onPurchaseResponse(PurchaseResponse paramPurchaseResponse)
  {
    while (true)
    {
      Request localRequest;
      DatabaseHelper localDatabaseHelper;
      PaymentTransaction localPaymentTransaction;
      Response localResponse;
      try
      {
        Log.v("Amazon-IAP", "onPurchaseResponse recieved");
        Log.v("Amazon-IAP", "PurchaseRequestStatus:" + paramPurchaseResponse.getPurchaseRequestStatus());
        String str1 = this.mUserId;
        String str2 = paramPurchaseResponse.getUserId();
        if (!str2.equals(str1))
        {
          this.mUserId = str2;
          PurchasingManager.initiatePurchaseUpdatesRequest(Offset.fromString(this.mContext.getSharedPreferences(this.mUserId, 0).getString("offset", Offset.BEGINNING.toString())));
        }
        final String str3 = paramPurchaseResponse.getRequestId();
        PaymentManager localPaymentManager = PaymentManager.getInstance();
        localRequest = (Request)localPaymentManager.getActiveRequest(new RequestByIdFilter().setId(str3));
        if (localRequest == null)
          return;
        localDatabaseHelper = DatabaseHelper.getHelper(EngineActivity.getInstance());
        PaymentTransaction.dump(localDatabaseHelper.getTransactions());
        DatabaseHelper.IObjectFilter local1 = new DatabaseHelper.IObjectFilter()
        {
          public boolean filter(Object paramAnonymousObject)
          {
            PaymentTransaction localPaymentTransaction = (PaymentTransaction)paramAnonymousObject;
            if (!PurchasingObserver.this.mPaymentSystem.getName().equals(localPaymentTransaction.paymentSystem))
              return false;
            return localPaymentTransaction.customData.equals(str3);
          }
        };
        List localList = localDatabaseHelper.getTransactions(local1);
        if (localList.size() > 1)
          Log.d("Amazon-IAP", "transactions != 1");
        localPaymentTransaction = (PaymentTransaction)localList.get(0);
        localResponse = new Response();
        localResponse.setListener(this.mPaymentSystem.getDefaultListener());
        localResponse.setSku(localRequest.getSku());
        localResponse.setName(localRequest.getName());
        switch (3.$SwitchMap$com$amazon$inapp$purchasing$PurchaseResponse$PurchaseRequestStatus[paramPurchaseResponse.getPurchaseRequestStatus().ordinal()])
        {
        default:
          localPaymentManager.postResponse(localResponse);
          localPaymentManager.removeActiveRequest(localRequest);
          return;
        case 1:
        case 2:
        case 3:
        case 4:
        }
      }
      catch (Exception localException)
      {
        Error.processException("amazon-onPurchaseResponse", localException);
        return;
      }
      Receipt localReceipt = paramPurchaseResponse.getReceipt();
      if (localReceipt != null);
      switch (3.$SwitchMap$com$amazon$inapp$purchasing$Item$ItemType[localReceipt.getItemType().ordinal()])
      {
      case 1:
      case 2:
      default:
        localPaymentTransaction.time = System.currentTimeMillis();
        localResponse.setInternalId(localDatabaseHelper.saveTransaction(localPaymentTransaction));
        localResponse.setSku(localReceipt.getSku());
        localResponse.setStatus(Response.Status.PURCHASE);
        break;
      case 3:
        throw new Exception("Amazon Subscriptions is not supported!");
        Log.v("Amazon-IAP", "Failed purchase for request" + localRequest);
        localDatabaseHelper.deleteTransaction(localPaymentTransaction.internalId);
        localResponse.setErrorCode(PurchaseResponse.PurchaseRequestStatus.FAILED.ordinal());
        localResponse.setStatus(Response.Status.ERROR);
        continue;
        Log.v("Amazon-IAP", "Invalid Sku for request " + localRequest);
        localDatabaseHelper.deleteTransaction(localPaymentTransaction.internalId);
        localResponse.setErrorCode(PurchaseResponse.PurchaseRequestStatus.INVALID_SKU.ordinal());
        localResponse.setStatus(Response.Status.ERROR);
      }
    }
  }

  public void onPurchaseUpdatesResponse(PurchaseUpdatesResponse paramPurchaseUpdatesResponse)
  {
    SharedPreferences.Editor localEditor;
    DatabaseHelper localDatabaseHelper;
    List localList;
    try
    {
      Log.v("Amazon-IAP", "onPurchaseUpdatesRecived recieved: Response -" + paramPurchaseUpdatesResponse);
      Log.v("Amazon-IAP", "PurchaseUpdatesRequestStatus:" + paramPurchaseUpdatesResponse.getPurchaseUpdatesRequestStatus());
      Log.v("Amazon-IAP", "RequestID:" + paramPurchaseUpdatesResponse.getRequestId());
      localEditor = getSharedPreferencesEditor();
      String str1 = this.mUserId;
      if (!paramPurchaseUpdatesResponse.getUserId().equals(str1))
        return;
      localDatabaseHelper = DatabaseHelper.getHelper(this.mContext);
      DatabaseHelper.IObjectFilter local2 = new DatabaseHelper.IObjectFilter()
      {
        public boolean filter(Object paramAnonymousObject)
        {
          return "amazon".equals(((PaymentTransaction)paramAnonymousObject).paymentSystem);
        }
      };
      localList = localDatabaseHelper.getTransactions(local2);
      Iterator localIterator1 = paramPurchaseUpdatesResponse.getRevokedSkus().iterator();
      if (localIterator1.hasNext())
      {
        String str3 = (String)localIterator1.next();
        Log.v("Amazon-IAP", "Revoked Sku:" + str3);
        throw new Exception("Amaozn revoke is not supported");
      }
    }
    catch (Exception localException)
    {
      Error.processException("Amazon-IAP", localException);
      return;
    }
    if (paramPurchaseUpdatesResponse.getPurchaseUpdatesRequestStatus() != PurchaseUpdatesResponse.PurchaseUpdatesRequestStatus.FAILED)
    {
      Iterator localIterator2 = paramPurchaseUpdatesResponse.getReceipts().iterator();
      while (localIterator2.hasNext())
      {
        Receipt localReceipt = (Receipt)localIterator2.next();
        String str2 = localReceipt.getSku();
        if (localReceipt.getItemType() == Item.ItemType.ENTITLED)
        {
          Iterator localIterator3 = localList.iterator();
          PaymentTransaction localPaymentTransaction1;
          do
          {
            boolean bool = localIterator3.hasNext();
            localObject = null;
            if (!bool)
              break;
            localPaymentTransaction1 = (PaymentTransaction)localIterator3.next();
          }
          while ((!"amazon".equals(localPaymentTransaction1.paymentSystem)) || (!localPaymentTransaction1.paymentId.equals(str2)));
          Object localObject = localPaymentTransaction1;
          if (localObject == null)
          {
            PaymentManager localPaymentManager = PaymentManager.getInstance();
            PaymentTransaction localPaymentTransaction2 = new PaymentTransaction();
            localPaymentTransaction2.paymentId = localPaymentManager.getRequestNameBySku("amazon", str2);
            localPaymentTransaction2.paymentSystem = "amazon";
            localPaymentTransaction2.quantity = 1;
            localPaymentTransaction2.time = System.currentTimeMillis();
            localPaymentTransaction2.customData = localReceipt.getPurchaseToken();
            localDatabaseHelper.saveTransaction(localPaymentTransaction2);
          }
        }
      }
      Offset localOffset = paramPurchaseUpdatesResponse.getOffset();
      localEditor.putString("offset", localOffset.toString());
      localEditor.commit();
      if (paramPurchaseUpdatesResponse.isMore())
      {
        Log.v("Amazon-IAP", "Initiating Another Purchase Updates with offset: " + localOffset.toString());
        PurchasingManager.initiatePurchaseUpdatesRequest(localOffset);
      }
    }
  }

  public void onSdkAvailable(boolean paramBoolean)
  {
    String str = PurchasingManager.initiateGetUserIdRequest();
    Log.v("Amazon-IAP", "onSdkAvailable recieved: Response -" + paramBoolean + " :" + str);
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.melesta.payment.amazon.PurchasingObserver
 * JD-Core Version:    0.6.2
 */